package org.example;

/**
 * 如果当前类是一个匿名内部类
 * getDeclaringClass 声明的类:返回为null
 * getEnclosingClass  包裹的类,返回的是外部类NormalClazz
 */
public class AnonymousLocalClazz {

    interface MyInterface {
        public abstract void sayHello();
    }

    public MyInterface getInterfaceInstance() {

        return new MyInterface() {
            public void sayHello() {
                System.out.println("this.class.getEnclosingClass:" + this.getClass().getEnclosingClass());
                System.out.println("this.class.getDeclaringClass:" + this.getClass().getDeclaringClass());
                System.out.println("this.getClass:" + this.getClass());
                System.out.println("this.getClass.getPackage:" + this.getClass().getPackage());
            }
        };

    }


    public static void main(String args[]) throws Exception {
        AnonymousLocalClazz clazz = new AnonymousLocalClazz();
        MyInterface inst = clazz.getInterfaceInstance();
        inst.sayHello();

        System.out.println();

        // 注意:实现类和接口属于不同的class信息
        System.out.println(inst.getClass());//返回 org.example.NormalClazz$1
        System.out.println(inst.getClass().getPackage());//返回 package org.example
        System.out.println(MyInterface.class);//返回 org.example.NormalClazz$MyInterface
    }

}
